# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("@io_bazel_rules_docker//container:container.bzl", "container_bundle")
load("@io_bazel_rules_docker//contrib:push-all.bzl", "container_push")
load("//bazel:images.bzl", "generate_vizier_metadata_persist_yamls", "generate_vizier_yamls", "image_map_with_bundle_version")

package(default_visibility = ["//visibility:public"])

VIZIER_IMAGE_MAP = {
    "gcr.io/pixie-oss/pixie-dev/vizier/cert_provisioner_image": "//src/utils/cert_provisioner:cert_provisioner_image",
    "gcr.io/pixie-oss/pixie-dev/vizier/certmgr_server_image": "//src/vizier/services/certmgr:certmgr_server_image",
    "gcr.io/pixie-oss/pixie-dev/vizier/cloud_connector_server_image": "//src/vizier/services/cloud_connector:cloud_connector_server_image",
    "gcr.io/pixie-oss/pixie-dev/vizier/kelvin_image": "//src/vizier/services/agent/kelvin:kelvin_image",
    "gcr.io/pixie-oss/pixie-dev/vizier/metadata_server_image": "//src/vizier/services/metadata:metadata_server_image",
    "gcr.io/pixie-oss/pixie-dev/vizier/pem_image": "//src/vizier/services/agent/pem:pem_image",
    "gcr.io/pixie-oss/pixie-dev/vizier/query_broker_server_image": "//src/vizier/services/query_broker:query_broker_server_image",
    "gcr.io/pixie-oss/pixie-dev/vizier/vizier_updater_image": "//src/utils/pixie_updater:vizier_updater_image",
}

public_image_replacement = {"gcr.io/pixie-oss/pixie-dev": "gcr.io/pixie-oss/pixie-prod"}

generate_vizier_yamls(
    name = "public_vizier_etcd_metadata_prod",
    srcs = glob(["**/*.yaml"]),
    out = "public_vizier_etcd_metadata_prod.yaml",
    image_map = VIZIER_IMAGE_MAP,
    replace = public_image_replacement,
)

generate_vizier_yamls(
    name = "private_vizier_etcd_metadata_prod",
    srcs = glob(["**/*.yaml"]),
    out = "private_vizier_etcd_metadata_prod.yaml",
    image_map = VIZIER_IMAGE_MAP,
    replace = {},
)

generate_vizier_metadata_persist_yamls(
    name = "public_vizier_metadata_persist_prod",
    srcs = glob(["**/*.yaml"]),
    out = "public_vizier_metadata_persist_prod.yaml",
    image_map = VIZIER_IMAGE_MAP,
    replace = public_image_replacement,
)

generate_vizier_metadata_persist_yamls(
    name = "private_vizier_metadata_persist_prod",
    srcs = glob(["**/*.yaml"]),
    out = "private_vizier_metadata_persist_prod.yaml",
    image_map = VIZIER_IMAGE_MAP,
    replace = {},
)

container_bundle(
    name = "private_vizier_images_bundle",
    images = image_map_with_bundle_version(
        image_map = VIZIER_IMAGE_MAP,
        replace = {},
        tag_latest = False,
    ),
)

container_bundle(
    name = "public_vizier_images_bundle",
    images = image_map_with_bundle_version(
        image_map = VIZIER_IMAGE_MAP,
        replace = public_image_replacement,
        tag_latest = False,
    ),
)

container_push(
    name = "vizier_images_push",
    bundle = select({
        ":public": ":public_vizier_images_bundle",
        "//conditions:default": ":private_vizier_images_bundle",
    }),
    format = "Docker",
)

config_setting(
    name = "public",
    values = {"define": "public=True"},
)

pkg_tar(
    name = "vizier_yamls",
    srcs =
        select({
            ":public": [
                "//k8s/vizier:public_vizier_etcd_metadata_prod.yaml",
                "//k8s/vizier:public_vizier_metadata_persist_prod.yaml",
            ],
            "//conditions:default": [
                "//k8s/vizier:private_vizier_etcd_metadata_prod.yaml",
                "//k8s/vizier:private_vizier_metadata_persist_prod.yaml",
            ],
        }) + [
            "//k8s/vizier_deps:nats_prod",
            "//k8s/vizier_deps:etcd_prod",
        ],
    package_dir = "/yamls",
    remap_paths = {
        "/vizier/private_vizier_etcd_metadata_prod.yaml": "vizier/vizier_etcd_metadata_prod.yaml",
        "/vizier/private_vizier_metadata_persist_prod.yaml": "vizier/vizier_metadata_persist_prod.yaml",
        "/vizier/public_vizier_etcd_metadata_prod.yaml": "vizier/vizier_etcd_metadata_prod.yaml",
        "/vizier/public_vizier_metadata_persist_prod.yaml": "vizier/vizier_metadata_persist_prod.yaml",
    },
    strip_prefix = "/k8s",
)
